package com.estar.assistsurvy.activity;

import java.util.Calendar;

import com.estar.assistsurvy.R;
import com.estar.assistsurvy.entity.ExperienceInfoList;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class AddExperienceActivity extends BaseActivity implements OnClickListener,OnTouchListener{

	private TextView startTime,endTime,returnHome;
	private EditText company;
	private Button save;
	private Intent intent;
	private InputMethodManager imm;
	private Calendar calendar;
	private ExperienceInfoList experienceInfo;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_experiences);
		initView();
	}

	private void initView(){
		returnHome=find(R.id.returnBack);
		returnHome.setOnClickListener(this);
		startTime=find(R.id.startTime);
		startTime.setOnTouchListener(this);
		endTime=find(R.id.endTime);
		endTime.setOnTouchListener(this);
		company=find(R.id.company);
		save=find(R.id.save);
		save.setOnClickListener(this);
		intent=getIntent();
		imm=(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		calendar=Calendar.getInstance();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.returnBack:
			finish();
			break;

		case R.id.save:
			if(!startTime.getText().toString().trim().equals("") && 
					!endTime.getText().toString().trim().equals("") && 
					!company.getText().toString().trim().equals("")){
				experienceInfo=new ExperienceInfoList();
				experienceInfo.setBeginTime(startTime.getText().toString().trim());
				experienceInfo.setEndTime(endTime.getText().toString().trim());
				experienceInfo.setCompany(company.getText().toString().trim());
				intent.putExtra("experienceInfo", experienceInfo);
				setResult(2, intent);
				finish();
			}else{
			
				Toast.makeText(AddExperienceActivity.this, "请将信息填写完整", Toast.LENGTH_SHORT).show();	
			}
			
			break;

		default:
			break;
		}
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		if(event.getAction()==MotionEvent.ACTION_DOWN){
			imm.hideSoftInputFromWindow(v.getWindowToken(), 0); //强制隐藏键盘  
			switch (v.getId()) {
			case R.id.startTime:
				new DatePickerDialog(AddExperienceActivity.this, AlertDialog.THEME_HOLO_LIGHT, new DatePickerDialog.OnDateSetListener() {

					@Override
					public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
						// TODO Auto-generated method stub
						// 更新EditText控件日期 小于10加0
						startTime.setText(new StringBuilder()
								.append(year)
								.append("-")
								.append((month + 1) < 10 ? "0"
										+ (month + 1) : (month + 1))
								.append("-")
								.append((dayOfMonth < 10) ? "0" + dayOfMonth : dayOfMonth));
						endTime.requestFocus();  
					}
				}, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();

				break;

			case R.id.endTime:
				new DatePickerDialog(AddExperienceActivity.this, AlertDialog.THEME_HOLO_LIGHT, new DatePickerDialog.OnDateSetListener() {

					@Override
					public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
						// TODO Auto-generated method stub
						// 更新EditText控件日期 小于10加0
						endTime.setText(new StringBuilder()
								.append(year)
								.append("-")
								.append((month + 1) < 10 ? "0"
										+ (month + 1) : (month + 1))
								.append("-")
								.append((dayOfMonth < 10) ? "0" + dayOfMonth : dayOfMonth));
						company.requestFocus();  
					}
				}, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();

				break;

			default:
				break;
			}
		}
		return true;
	}
}
